<template>
  <div>
    <!-- 导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <div style="margin: 10px 0">
    <el-button type="primary" style="margin-right: 20px" @click="addopen"
      ><el-icon><Plus /></el-icon>上传</el-button
    >
  </div>
  <el-table :data="carouselList" border style="width: 100%">
    <el-table-column type="index"></el-table-column>
    <el-table-column label="轮播图名称" prop="carouselname" />
    <el-table-column label="轮播图预览" >
        <template #default="scope">
          <img
            v-if="scope.row.carouselpath"
            :src="scope.row.carouselpath"
            style="height: 30px; width: 30px; border-radius: 50%"
          />
          <span v-else>暂无</span>
        </template>
    </el-table-column>
    <el-table-column label="轮播图详情" prop="carouseldesc" />
    <el-table-column align="center" label="操作">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row.id)"
          >修 改</el-button
        >
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.row.id)"
          >删 除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      v-model:current-page="pageNum"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
  <!-- 新增区域 -->
  <el-dialog v-model="dialogVisible" title="轮播图信息" width="30%">
      <el-form label-width="90px" size="small">
        <el-form-item label="轮播图名称">
          <el-input size="small" v-model="form.carouselname" type="text" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上传轮播图">
            <el-upload
            class="avatar-uploader"
            ref="upload"
            action="#"
            :show-file-list="false"
            :on-change="handleChange"
            :auto-upload="false"
            list-type="picture-card"
          >
            <img
              v-if="form.carouselpath"
              :src="form.carouselpath"
              class="avatar"
              width="148"
              height="148"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="轮播图详情">
          <el-input size="small" v-model="form.carouseldesc" type="text" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script>
export default {
  name: "carouselView",
  data() {
    return {
      carouselList: [],
      dialogVisible: false,
      form: {},
      pageNum: 1,
      pageSize: 10,
      total: 0,
    };
  },
  created() {
    this.getcarouselList();
  },
  methods: {
    handleChange(file) {
      const isJPG = file.raw.type.indexOf("image") !== -1;
      const isLt2M = file.raw.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
        return;
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
        return;
      }
      this.form.carouselpath = file.url;
      this.file = file;
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.getcarouselList();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.getcarouselList();
    },
    addopen() {
      this.form = {};
      this.dialogVisible = true;
    },
    //获取原本的数据
    handleEdit(carouselid) {
      this.dialogVisible = true;
      this.$http.get("/carousel/" + carouselid).then((res) => {
        if (res.data.status == 200) {
          this.form = res.data.data;
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    //删除用户方法
    handleDelete(carouselid) {
      this.$http.delete("/carousel/" + carouselid).then((res) => {
        if (res.data.status == 200) {
          this.$message.success("删除成功"), this.getcarouselList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    //修改新增同一个方法
    save() {
      let fromdata = new FormData();
      fromdata.append("file", this.file.raw);
      this.$http.post("/oss/upload", fromdata).then((ress) => {
        if (ress.data.status == 200) {
          this.form.carouselpath = ress.data.data;

          this.$http.post("/carousel/", this.form).then((res) => {
            if (res.data.status == 200) {
              this.$message.success("保存成功"),
                (this.dialogVisible = false),
                this.getcarouselList();
            } else {
              this.$message.error(res.data.message);
            }
          });
        } else {
          this.$message.error(ress.data.message);
          return false;
        }
      });
    },
    getcarouselList() {
      //请求分页查询数据
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      this.$http.get("/carousel/page", { params }).then((res) => {
        console.log(res);
        if (res.data.status == 200) {
          this.carouselList = res.data.data.records;
          this.total = res.data.data.total;
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
  },
};
</script>
<style>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  line-height: 128px;
  text-align: center;
}

.avatar {
  display: block;
}
</style>